<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">


<!-- remove old header -->


<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>cogen API Reference Documentation
        - Module: core</title><meta name="MSSmartTagsPreventParsing" content="true" /><meta http-equiv="imagetoolbar" content="no" /><link href="css/default.css" rel="stylesheet" type="text/css" /><link href="css/pygments.css" rel="stylesheet" type="text/css" />
    <!-- <link rel="shortcut icon" href="/images/favicon.ico" type="image/vnd.microsoft.icon"/> -->
    <link rel="stylesheet" href="css/apydia.css" type="text/css" />
    <script src="js/jquery-1.2.1.pack.js" type="text/javascript"></script>
    <script src="js/apydia.js" type="text/javascript"></script>
	<link rel="icon" href="images/favicon.ico" type="image/x-icon" />
</head>
<body>
    <div id="page">
        <div id="browser">
            <div class="browser-section">
                <h2>Module Index</h2>
                <ul>
                    <li>
                        <a href="cogen.html">cogen</a>
                    </li><li>
                        <a href="cogen.common.html">cogen.common</a>
                    </li><li class="active">
                        <a href="cogen.core.html">cogen.core</a>
                    </li><li>
                        <a href="cogen.core.coroutines.html">cogen.core.coroutines</a>
                    </li><li>
                        <a href="cogen.core.events.html">cogen.core.events</a>
                    </li><li>
                        <a href="cogen.core.pubsub.html">cogen.core.pubsub</a>
                    </li><li>
                        <a href="cogen.core.queue.html">cogen.core.queue</a>
                    </li><li>
                        <a href="cogen.core.reactors.html">cogen.core.reactors</a>
                    </li><li>
                        <a href="cogen.core.reactors.base.html">cogen.core.reactors.base</a>
                    </li><li>
                        <a href="cogen.core.reactors.iocp_proactor.html">cogen.core.reactors.iocp_proactor</a>
                    </li><li>
                        <a href="cogen.core.reactors.poll_reactor.html">cogen.core.reactors.poll_reactor</a>
                    </li><li>
                        <a href="cogen.core.reactors.qt_reactor.html">cogen.core.reactors.qt_reactor</a>
                    </li><li>
                        <a href="cogen.core.reactors.select_reactor.html">cogen.core.reactors.select_reactor</a>
                    </li><li>
                        <a href="cogen.core.schedulers.html">cogen.core.schedulers</a>
                    </li><li>
                        <a href="cogen.core.sockets.html">cogen.core.sockets</a>
                    </li><li>
                        <a href="cogen.core.util.html">cogen.core.util</a>
                    </li><li>
                        <a href="cogen.web.html">cogen.web</a>
                    </li><li>
                        <a href="cogen.web.async.html">cogen.web.async</a>
                    </li><li>
                        <a href="cogen.web.wsgi.html">cogen.web.wsgi</a>
                    </li>
                </ul>
            </div>
            <div class="browser-section">
                <h2>Submodules</h2>
                <ul>
                    <li>
                        <a href="cogen.core.coroutines.html">coroutines</a>
                    </li><li>
                        <a href="cogen.core.events.html">events</a>
                    </li><li>
                        <a href="cogen.core.pubsub.html">pubsub</a>
                    </li><li>
                        <a href="cogen.core.queue.html">queue</a>
                    </li><li>
                        <a href="cogen.core.reactors.html">reactors</a>
                    </li><li>
                        <a href="cogen.core.schedulers.html">schedulers</a>
                    </li><li>
                        <a href="cogen.core.sockets.html">sockets</a>
                    </li><li>
                        <a href="cogen.core.util.html">util</a>
                    </li>
                </ul>
            </div>
        </div>
        <div id="content">
    <h1>Module:
        cogen.core
    </h1>
    <div class="docstring rst"><p>This module holds the essential stuff.</p>
<p>Programming with this library should be straghtforward. A coroutine is just
a generator wrapped in a operation handling class:</p>
<div class="source"><pre><span class="nd">@coroutine</span>
<span class="k">def</span> <span class="nf">mycoro</span><span class="p">(</span><span class="n">bla</span><span class="p">):</span>
    <span class="k">yield</span> <span class="o">&lt;</span><span class="n">operation</span><span class="o">&gt;</span>
    <span class="k">yield</span> <span class="o">&lt;</span><span class="n">operation</span><span class="o">&gt;</span>
</pre></div>
<ul class="simple">
<li>the <cite>operation</cite> instructs the scheduler what to do with the
coroutine: suspend it till someting happens, add another coro in
the scheduler, raise a event and so on.</li>
<li>the <cite>operations</cite> are split up in 2 modules: events and sockets<ul>
<li>the <cite>operations</cite> from sockets are related to network, like reading and
writing, and these are done asynchronously but your code in the
coroutine will see them as a regular synchronous or blocking call.</li>
<li>the <cite>operations</cite> from events are related to signals and
coroutine/scheduler management.</li>
</ul>
</li>
<li>if a <cite>operation</cite> has a result associated then the yield will return that
result (eg. a string or a (connection, address) tuple) otherwise it will
return the operation instance.</li>
</ul>
<p>Typical example:</p>
<div class="source"><pre><span class="k">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span>
<span class="k">from</span> <span class="nn">cogen.common</span> <span class="k">import</span> <span class="o">*</span>

<span class="nd">@coroutine</span>
<span class="k">def</span> <span class="nf">server</span><span class="p">():</span>
    <span class="n">srv</span> <span class="o">=</span> <span class="n">sockets</span><span class="o">.</span><span class="n">Socket</span><span class="p">()</span>
    <span class="n">srv</span><span class="o">.</span><span class="n">setblocking</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span>
    <span class="n">srv</span><span class="o">.</span><span class="n">bind</span><span class="p">((</span><span class="s">'localhost'</span><span class="p">,</span><span class="mf">777</span><span class="p">))</span>
    <span class="n">srv</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="mf">10</span><span class="p">)</span>
    <span class="k">while</span> <span class="mf">1</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">"Listening..."</span>
        <span class="n">conn</span><span class="p">,</span> <span class="n">addr</span> <span class="o">=</span> <span class="k">yield</span> <span class="n">sockets</span><span class="o">.</span><span class="n">Accept</span><span class="p">(</span><span class="n">srv</span><span class="p">)</span>
        <span class="k">print</span> <span class="s">"Connection from </span><span class="si">%s</span><span class="s">:</span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">addr</span>
        <span class="n">m</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">handler</span><span class="p">,</span> <span class="n">conn</span><span class="p">,</span> <span class="n">addr</span><span class="p">)</span>

<span class="nd">@coroutine</span>
<span class="k">def</span> <span class="nf">handler</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">addr</span><span class="p">):</span>
    <span class="k">yield</span> <span class="n">sockets</span><span class="o">.</span><span class="n">Write</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="s">"WELCOME TO ECHO SERVER !</span><span class="se">\r\n</span><span class="s">"</span><span class="p">)</span>
    <span class="k">while</span> <span class="mf">1</span><span class="p">:</span>
        <span class="n">line</span> <span class="o">=</span> <span class="k">yield</span> <span class="n">sockets</span><span class="o">.</span><span class="n">ReadLine</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="mf">8192</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s">'exit'</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">sockets</span><span class="o">.</span><span class="n">Write</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="s">"GOOD BYE"</span><span class="p">)</span>
            <span class="n">sock</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="k">return</span>

        <span class="k">yield</span> <span class="n">sockets</span><span class="o">.</span><span class="n">Write</span><span class="p">(</span><span class="n">sock</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span>

<span class="n">m</span> <span class="o">=</span> <span class="n">Scheduler</span><span class="p">()</span>
<span class="n">m</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">server</span><span class="p">)</span>
<span class="n">m</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</pre></div>
</div>
    <ul class="link-bar">
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/__init__.py#1">View source</a>
    </li>
</ul>
    <div>
        <h2>Modules</h2>
            <h3>
                <a href="cogen.core.coroutines.html">coroutines</a>
            </h3>
            <div class="docstring rst"><p>Coroutine related boilerplate and wrappers.</p>
</div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.coroutines.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/coroutines.py#1">View source</a>
    </li>
</ul>
            <h3>
                <a href="cogen.core.events.html">events</a>
            </h3>
            <div class="docstring rst"><p>Base events (coroutine operations) and coroutine exceptions.</p>
</div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.events.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/events.py#1">View source</a>
    </li>
</ul>
            <h3>
                <a href="cogen.core.pubsub.html">pubsub</a>
            </h3>
            <div class="docstring rst"></div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.pubsub.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/pubsub.py#1">View source</a>
    </li>
</ul>
            <h3>
                <a href="cogen.core.queue.html">queue</a>
            </h3>
            <div class="docstring rst"><p>Port of Queue.Queue from the python standard library.</p>
</div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.queue.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/queue.py#1">View source</a>
    </li>
</ul>
            <h3>
                <a href="cogen.core.reactors.html">reactors</a>
            </h3>
            <div class="docstring rst"><p>Network polling code.</p>
<p>The reactor works in tandem with the socket operations.
Here's the basic workflow:</p>
<ul class="simple">
<li>the coroutine yields a operation</li>
<li>the scheduler runs that operation (the <a class="reference" href="cogen.core.events.Operation.html#method-process">process</a> method)
Note: all the socket operations share the same <a class="reference" href="cogen.core.sockets.SocketOperation.html#method-process">process method</a>.<ul>
<li>if run_first is False then the operation is added in the reactor for
polling (with the exception that if we have data in out internal buffers
the operation is runned first)</li>
<li>if run_first is set (it's default) in the operation then in process
method the reactor's <a class="reference" href="cogen.core.reactors.ReactorBase#method-run_or_add">run_or_add</a> is called with the
operation and coroutine</li>
</ul>
</li>
</ul>
<p>Note: run_first is a optimization hack really, first it tries to run the
operation (this asumes the sockets are usualy ready) and if it raises any
exceptions like EAGAIN, EWOULDBLOCK etc it adds that operation for polling
(via select, epoll, kqueue etc) then the run method will be called only when
select, epoll, kqueue says that the socket is ready.</p>
</div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.reactors.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/reactors/__init__.py#1">View source</a>
    </li>
</ul>
            <h3>
                <a href="cogen.core.schedulers.html">schedulers</a>
            </h3>
            <div class="docstring rst"><p>Scheduling framework.</p>
<p>The scheduler handles the timeouts, run the operations and does very basic
management of coroutines. Most of the heavy logic is in each operation class.
See: <a class="reference" href="cogen.core.events.html">events</a> and <a class="reference" href="cogen.core.sockets.html">sockets</a>.
Most of those operations work with attributes we set in the scheduler.</p>
<p><cite>cogen</cite> is multi-state. All the state related to coroutines and network is in
the scheduler and it's associated reactor. That means you could run several
cogen schedulers in the same process/thread/whatever.</p>
<p>There is just one thing that uses global objects - the threadlocal-like local
object in the coroutines module.  It was actually aded for the wsgiserver
factory that monkey patches the threadlocal module in order to make pylons run
correctly (pylons relies heavily on threadlocals).</p>
</div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.schedulers.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/schedulers.py#1">View source</a>
    </li>
</ul>
            <h3>
                <a href="cogen.core.sockets.html">sockets</a>
            </h3>
            <div class="docstring rst"><p>Socket-only coroutine operations and <cite>Socket</cite> wrapper.
Really - the only thing you need to know for most stuff is
the <a class="reference" href="cogen.core.sockets.Socket.html">Socket</a> class.</p>
</div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.sockets.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/sockets.py#1">View source</a>
    </li>
</ul>
            <h3>
                <a href="cogen.core.util.html">util</a>
            </h3>
            <div class="docstring rst"><p>Mischelaneous or common.</p>
</div>
            <ul class="link-bar">
    <li>
        <a href="cogen.core.util.html">Details</a>
    </li>
    <li class="link-to-sourcecode">
        <a href="http://code.google.com/p/cogen/source/browse/trunk/cogen/core/util.py#1">View source</a>
    </li>
</ul>
    </div>
        </div>
        <div id="footer">
            Generated with <a href="http://apydia.ematia.de/" class="extern">
            Apydia 0.0.3</a>
        </div>
    </div>
</body>
</html>